<template>
  <div class="app-container">
    <div class="syl-group">
      <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.native.prevent>
        <el-form-item label="原料名称:" prop="materialName">
          <el-input
            v-model.trim="queryParams.materialName"
            placeholder="请输入原料名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" size="mini" class="ordinary" style="margin-right: 20px" @click="resetQuery">重置</el-button>
          <el-button type="primary" size="mini" @click="handleAdd">新增原料</el-button>
        </el-form-item>
      </el-form>
      <div v-if="equWristbandList.length>0" style="width: 100%">
        <el-card v-for="item in equWristbandList" :key="item.id" class="syl-card" shadow="never">
          <div class="syl-upper" v-html="item.materialName">青椒</div>
          <div class="syl-bottom">
            <div class="syl-bottom-left" v-html="'规格:'+item.unitName" />
            <div class="syl-bottom-right">
              <el-button size="mini" type="warning" round style="width: 72px" @click="handleUpdate(item)">编 辑</el-button>
            </div>
          </div>
        </el-card>
      </div>
      <el-row v-else :gutter="20">
        <h3>暂无数据</h3>
      </el-row>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改原料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="原料名称" prop="materialName">
          <el-input v-model.trim="form.materialName" placeholder="请输入原料名称" style="width: 260px" :maxlength="10" />
        </el-form-item>
        <el-form-item label="原料单位" prop="unitId">
          <el-select v-model="form.unitId" placeholder="请选择" style="width: 260px">
            <el-option
              v-for="item in makeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-divider direction="vertical" />
        <el-button v-preventReClick type="primary" :loading="innerVisible" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getMathList, addMathSave } from '@/api/commodity/material'
import { getMate } from '@/api/commodity/classification'
export default {
  name: 'Index',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 单位
      makeList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 套餐表格数据
      equWristbandList: [],
      // 状态
      options: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      //  嵌套弹出层
      innerVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialName: undefined
      },
      imageUrl: '',
      // 表单参数
      form: {
        materialName: undefined,
        unitId: undefined
      },
      // 表单校验
      rules: {
        materialName: [
          { required: true, message: '原料名称不能为空', trigger: 'blur' },
          {
            pattern: /[\u4e00-\u9fa5]/,
            message: '原料名称只能为中文',
            trigger: 'blur'
          }
        ],
        unitId: [
          { required: true, message: '原料单位不能为空', trigger: 'blur' }
        ]
      },
      form1: {},
      rules1: {}
    }
  },
  created() {
    this.getList()
    this.getDateList()
  },
  methods: {
    /** 查询菜单列表 */
    getList() {
      getMathList(this.queryParams).then(res => {
        const { total, records } = res.data
        this.total = total
        this.equWristbandList = records
      })
    },
    /** 获取原料计量列表 **/
    getDateList() {
      getMate(this.queryParams).then(res => {
        this.makeList = res.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.innerVisible = false
      this.form = {
        materialName: undefined,
        unitId: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增原料'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.title = '编辑原料'
      this.open = true
      const { materialName, unitId, id } = row
      this.form = {
        materialName: materialName,
        unitId: unitId,
        id: id
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.innerVisible = true
          addMathSave(this.form).then(res => {
            if (!res.code) {
              this.open = false
              this.innerVisible = false
              this.getList()
              this.msgSuccess('操作成功')
            }
          })
        }
      })
      setTimeout(() => {
        this.innerVisible = false
      }, 3000)
    }
  }
}
</script>
<style lang="scss" scoped>
/** 卡片样式 **/
.syl-card {
  width: 243px;
  height: 120px;
  margin: 10px 10px;
  display: inline-block;
  background: #FFFFFF;
  border-radius: 10px;
  box-shadow: 0 2px 11px 7px rgba(238, 238, 238, 0.5);
  .syl-upper {
    width: 100%;
    height: 40px;
    line-height: 40px;
    font-size: 16px;
    font-weight: 600;
  }
  .syl-bottom {
    width: 100%;
    height: 80px;
    display: flex;
    font-size: 14px;
    line-height: 50px;

    .syl-bottom-left {
      width: 100%;
      height: 80px;
      text-align: left;
    }

    .syl-bottom-right {
      width: 100%;
      height: 80px;
      text-align: right;
    }
  }
}
/deep/.el-divider--vertical{
  margin: 6px 15px;
}

</style>
